package cc.flybb.roombasic_step;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Database(entities = {Word.class}, version = 1, exportSchema = false)
public abstract class WordDatabase extends RoomDatabase {

    public abstract WordDao getWordDao();

    private static volatile WordDatabase INSTANCE;
    private static final int NUMBER_OF_THREADS = 4;
    static final ExecutorService databaseWriteExecutor =
            Executors.newFixedThreadPool(NUMBER_OF_THREADS);

    static WordDatabase getDatabase(final Context context) {
        if (INSTANCE == null) {
            synchronized (WordDatabase.class) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                            WordDatabase.class, "word_database")
                            .build();
                }
            }
        }
        return INSTANCE;
    }

//    private static WordDatabase INSTANCE;
//
//    static synchronized WordDatabase getDatabase(Context context) {
//        if (INSTANCE == null) {
//            INSTANCE = Room.databaseBuilder(context.getApplicationContext(), WordDatabase.class, "word_database")
//                    //.fallbackToDestructiveMigration()
//                    //.addMigrations(MIGRATION_4_5)
//                    .build();
//        }
//        return INSTANCE;
//    }
//
//    public abstract WordDao getWordDao();

    //    static final Migration MIGRATION_2_3 = new Migration(2,3) {
//        @Override
//        public void migrate(@NonNull SupportSQLiteDatabase database) {
//            database.execSQL("ALTER TABLE word ADD COLUMN bar_data INTEGER NOT NULL DEFAULT 1");
//        }
//    };
//
//    static final Migration MIGRATION_3_4 = new Migration(3,4) {
//        @Override
//        public void migrate(@NonNull SupportSQLiteDatabase database) {
//            database.execSQL("CREATE TABLE word_temp (id INTEGER PRIMARY KEY NOT NULL ,english_word TEXT," +
//                    "chinese_meaning TEXT)");
//            database.execSQL("INSERT INTO word_temp (id,english_word,chinese_meaning) " +
//                    "SELECT id,english_word,chinese_meaning FROM word");
//            database.execSQL("DROP TABLE word");
//            database.execSQL("ALTER TABLE word_temp RENAME to word");
//        }
//    };
//
//    static final Migration MIGRATION_4_5 = new Migration(4,5) {
//        @Override
//        public void migrate(@NonNull SupportSQLiteDatabase database) {
//            database.execSQL("ALTER TABLE word ADD COLUMN  chinese_invisible INTEGER NOT NULL DEFAULT 0");
//        }
//    };

}
